# 导包
import unittest, requests


# 创建测试类 继承unittest.TestCase
class TestLogin(unittest.TestCase):
    # 编写测试固件
    def setUp(self):
        # 实例化session
        self.session = requests.Session()

    def tearDown(self):
        # 关闭session
        self.session.close()

    # 实现测试用例(testcase)
    def test01_login_success(self):
        # 使用session获取验证码,获取之后,验证码数据会自动保存到session中
        response = self.session.get(url="http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=verify")
        print("验证码响应头为: ", response.headers)
        # 断言验证码
        self.assertIn("image", response.headers.get("Content-Type"))
        # 使用session登录
        response = self.session.post(url="http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=do_login",
                                     headers={"Content-Type": "application/x-www-form-urlencoded"},
                                     data="username=13800138006&password=123456&verify_code=8888")
        # 打印结果
        print(response.json())
        # 断言登录的结果
        self.assertEqual(200, response.status_code)  # 断言响应状态码
        self.assertEqual(1, response.json().get("status"))  # 断言status
        self.assertIn("登陆成功", response.json().get("msg"))  # 断言msg

    def test02_username(self):
        # 使用session获取验证码,获取之后,验证码数据会自动保存到session中
        response = self.session.get(url="http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=verify")
        print("验证码响应头为: ", response.headers)
        # 断言验证码
        self.assertIn("image", response.headers.get("Content-Type"))
        # 使用session登录
        response = self.session.post(url="http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=do_login",
                                     headers={"Content-Type": "application/x-www-form-urlencoded"},
                                     data="username=14000138006&password=123456&verify_code=8888")
        # 打印结果
        print(response.json())
        # 断言登录的结果
        self.assertEqual(200, response.status_code)  # 断言响应状态码
        self.assertEqual(-1, response.json().get("status"))  # 断言status
        self.assertIn("账号不存在", response.json().get("msg"))  # 断言msg

    def test03_password_is_error(self):
        # 使用session获取验证码,获取之后,验证码数据会自动保存到session中
        response = self.session.get(url="http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=verify")
        print("验证码响应头为: ", response.headers)
        # 断言验证码
        self.assertIn("image", response.headers.get("Content-Type"))
        # 使用session登录
        response = self.session.post(url="http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=do_login",
                                     headers={"Content-Type": "application/x-www-form-urlencoded"},
                                     data="username=13800138006&password=1234567&verify_code=8888")
        # 打印结果
        print(response.json())
        # 断言登录的结果
        self.assertEqual(200, response.status_code)  # 断言响应状态码
        self.assertEqual(-2, response.json().get("status"))  # 断言status
        self.assertIn("密码错误", response.json().get("msg"))  # 断言msg
